using System.Drawing;

namespace Altairis.Nemesis.Gallery {
    internal static class Utils {

        public enum HorizontalAlign { Left, Center, Right }

        public enum VerticalAlign { Top, Middle, Bottom }

        public static Image ResizeImage(Image originalImage, Size newSize) {
            if (originalImage.Size == newSize) return originalImage;

            // Prepare canvas
            Size realNewSize = GetRealMaxSize(originalImage.Size, newSize);
            Bitmap targetImage = new Bitmap(realNewSize.Width, realNewSize.Height);
            Graphics gph = Graphics.FromImage(targetImage);
            gph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            // Draw image
            gph.DrawImage(originalImage, 0, 0, realNewSize.Width, realNewSize.Height);
            return targetImage;
        }

        public static Image ResizeImage(Image originalImage, Size newSize, Color backgroundColor) {
            return ResizeImage(originalImage, newSize, backgroundColor, HorizontalAlign.Center, VerticalAlign.Middle);
        }

        public static Image ResizeImage(Image originalImage, Size newSize, Color backgroundColor, HorizontalAlign hAlign, VerticalAlign vAlign) {
            if (originalImage.Size == newSize) return originalImage;

            // Prepare canvas
            Bitmap targetImage = new Bitmap(newSize.Width, newSize.Height);
            Graphics gph = Graphics.FromImage(targetImage);
            gph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            // Draw background
            gph.FillRectangle(new SolidBrush(backgroundColor), 0, 0, newSize.Width, newSize.Height);

            // Set size and position of image
            Size realNewSize = GetRealMaxSize(originalImage.Size, newSize);
            Point loc = new Point(0, 0);
            switch (vAlign) {
                case VerticalAlign.Middle:
                    loc.Y = (newSize.Height - realNewSize.Height) / 2;
                    break;
                case VerticalAlign.Top:
                    loc.Y = newSize.Height - realNewSize.Height;
                    break;
                default:
                    break;
            }
            switch (hAlign) {
                case HorizontalAlign.Center:
                    loc.X = (newSize.Width - realNewSize.Width) / 2;
                    break;
                case HorizontalAlign.Right:
                    loc.X = newSize.Width - realNewSize.Width;
                    break;
                default:
                    break;
            }

            // Draw image
            gph.DrawImage(originalImage, new Rectangle(loc, realNewSize));
            return targetImage;
        }

        private static Size GetRealMaxSize(SizeF originalSize, SizeF maxSize) {
            SizeF vRatio = new SizeF(maxSize.Width / originalSize.Width, maxSize.Height / originalSize.Height);
            float ratio = vRatio.Width < vRatio.Height ? vRatio.Width : vRatio.Height;
            return new Size(System.Convert.ToInt32(originalSize.Width * ratio), System.Convert.ToInt32(originalSize.Height * ratio));
        }
        
    }
}